/**
 * @author KujoMiva
 * @name CYS.ts
 * @desc 倡议书界面
 * @time 2020年02月11日 18:24:51 星期二
 */
class CYS extends egret.DisplayObjectContainer {
  private animationSpeed = 800
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }

  private init() {
    this.createBg()
    this.createCYS()
    this.createTalk()
    this.createBtnGroup()
    this.createHeart()
    this.createTip()
  }

  private createBg() {
    const bg = Utils.createBitmapByName('bg_cys_card_png')
    Utils.anchorCenter(bg)
    Utils.createScale(bg, this)
    bg.scaleX = bg.scaleX - .03
    bg.scaleY = bg.scaleY - .03
    bg.x = this.stage.stageWidth / 2
    bg.y = this.stage.stageHeight / 2
  }

  private createCYS() {
    const group = new eui.Group()
    const cys = new eui.Image(RES.getRes(`item_cys_png`))
    Utils.createScale(cys, group)
    const scrollView = new eui.Scroller()
    scrollView.viewport = group
    scrollView.width = 1116 * .47
    scrollView.height = scrollView.width - 100
    scrollView.anchorOffsetX = scrollView.width / 2
    scrollView.x = this.stage.stageWidth / 2
    scrollView.y = 200
    this.addChild(scrollView)
  }

  private createTalk() {
    const group = new eui.Group()
    // const arr = [1, 2, 3, 4, 5, 6, 7]
    const arr = ParamsPool.PLAYER_LIST || []
    for (let i = 0; i < arr.length; i++) {
      this.generateTalk(Number(i) + 1, group, arr[i])
    }

    const scrollView = new eui.Scroller()
    scrollView.viewport = group
    scrollView.width = 1116 * .47
    scrollView.height = scrollView.width - 250
    scrollView.anchorOffsetX = scrollView.width / 2
    scrollView.x = this.stage.stageWidth / 2
    scrollView.y = 200 + scrollView.width - 20
    this.addChild(scrollView)

    const mask = Utils.createBitmapByName('item_cys_mask_png')
    Utils.createScale(mask, this, .47)
    mask.anchorOffsetX = mask.width / 2
    mask.x = this.stage.stageWidth / 2
    mask.y = 890
  }

  private createBtnGroup() {
    const container = new egret.Sprite()

    const btn_back = Utils.createBitmapByName('btn_back_png')
    container.addChild(btn_back)
    btn_back.touchEnabled = true
    btn_back.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      this.stage.dispatchEvent(new egret.Event('gotoCheckMap'))
      this.parent.removeChild(this)
    }, this)

    const btn_next = Utils.createBitmapByName('btn_next_png')
    container.addChild(btn_next)
    btn_next.x = btn_back.width + 50
    btn_next.touchEnabled = true
    btn_next.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      this.dispatchEvent(new egret.Event('next'))
    }, this)

    Utils.anchorCenter(container)
    Utils.createScale(container, this)
    container.x = this.stage.stageWidth / 2
    container.y = 1030
  }


  private async generateTalk(id, con, data) {
    const group = new eui.Group()
    const container = new egret.Sprite()
    group.addChild(container)
    const cardGroup = new egret.Sprite()
    container.addChild(cardGroup)
    const card = Utils.createBitmapByName('item_talk_card_png')
    Utils.itemScale(card)
    cardGroup.addChild(card)
    const text = Utils.createBitmapByName(`item_text_${data.text}_png`)
    Utils.itemScale(text)
    cardGroup.addChild(text)
    Utils.anchorCenter(text)
    text.x = card.width / 2
    text.y = card.height / 2

    const avatar_mask = Utils.createBitmapByName(`item_avatar_png`)
    console.log(data.avatar)
    let avatar: any = await Utils.createBitmpByUrl(data.avatar)
    try {
      avatar = await Utils.createBitmpByUrl(data.avatar)
    } catch (error) {
      alert('获取头像失败,已启用备用头像')
      avatar = await Utils.createBitmpByUrl('https://thirdqq.qlogo.cn/qqapp/1104455702/4AFC34E4A90E0BB5B52FE0E374B6E249/100?imageView2/1/w/80/h/80')
    }
    avatar.width = avatar_mask.width
    avatar.height = avatar_mask.height
    Utils.itemScale(avatar_mask)
    container.addChild(avatar_mask)
    avatar_mask.y = 10
    Utils.itemScale(avatar)
    container.addChild(avatar)
    avatar.mask = avatar_mask
    avatar.y = 10
    if (id % 2 === 0) {
      avatar_mask.anchorOffsetX = avatar_mask.width
      avatar_mask.x = card.width
      avatar.anchorOffsetX = avatar.width
      avatar.x = card.width
    }

    const path = Utils.createBitmapByName(`item_path_text_${data.path}_png`)
    Utils.itemScale(path)
    container.addChild(path)
    path.y = 10
    if (id % 2 === 0) {
      path.anchorOffsetX = path.width
      path.x = card.width - avatar_mask.width - 20
    } else {
      path.x = avatar_mask.width + 20
    }

    group.y = container.height * (id - 1)
    group.height = container.height
    con.addChild(group)
  }

  private createHeart() {
    const heart = Utils.createBitmapByName('item_heart_png')
    Utils.createScale(heart, this, .05)
    Utils.anchorCenter(heart)
    heart.x = 320
    heart.y = 170
    heart.alpha = 0
    this.addEventListener('next', () => {
      egret.Tween.get(heart)
        .to({ scaleX: .15, scaleY: .15, x: this.stage.stageWidth / 2, y: this.stage.stageHeight / 2, alpha: 1 }, this.animationSpeed)
        .call(() => {
          ParamsPool.LOADING_GOTO = 'gotoOver'
          ParamsPool.LOADING_TIP = '正在生成分享图'
          this.stage.dispatchEvent(new egret.Event('gotoLoading'))
          console.log('gotoOver')
          egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
        })
    }, this)
  }

  private createTip() {
    const text = Utils.createText('拖动内容查看全部', { x: 0, y: 655 }, this, false)
    text.stroke = 0
    text.size = 20
    text.bold = false
    text.textColor = 0xffffff
    text.width = this.stage.stageWidth
    text.textAlign = egret.HorizontalAlign.CENTER
  }

}